import os
import requests
from lxml import etree
from comm.tdb import tdb, where
from comm.functons import node_xpath, config
from efficient.url import url_parse

tb = tdb.table('javbus')

domain = config('domain.javbus')


def xxx(page):
    rsp = requests.get(domain + '/uncensored/genre/hd/{}'.format(page))

    tree = etree.HTML(rsp.text)
    els = tree.xpath('//*[@class="movie-box"]')

    for node in els:
        row = {
            'img': domain + node_xpath(node, './/img/@src'),
            'code': node_xpath(node, './/date[1]/text()'),
            'date': node_xpath(node, './/date[2]/text()'),
            'local_img': '',
        }
        if not tb.search(where('code') == row['code']):
            tb.insert(row)
            print('ok', row['code'])
        else:
            print('已存在', row['code'])


def download_img():
    data = tb.search(where('local_img') == '')
    for row in data:
        file_path = '../resources' + url_parse(row['img'])

        if os.path.isfile(file_path):
            tb.update({'local_img': file_path}, where('code') == row['code'])
            print('图片已存在', row['code'])
            continue

        print('下载', row['img'], end=' ')
        rsp = requests.get(row['img'])

        if not os.path.exists(os.path.dirname(file_path)):
            os.makedirs(os.path.dirname(file_path))

        with open(file_path, 'wb+') as fp:
            fp.write(rsp.content)
        tb.update({'local_img': file_path}, where('code') == row['code'])
        print('成功')


if __name__ == '__main__':
    for i in range(1, 6):
        xxx(i)
    download_img()
    arr = tb.search(where('code') != '')
    print(len(arr))
